package logic

import (
	"context"
	"fmt"

	"go-zero_try/mall/order/rpc/pb/internal/svc"
	"go-zero_try/mall/order/rpc/pb/pb"
	"go-zero_try/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertSLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInsertSLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertSLogic {
	return &InsertSLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *InsertSLogic) InsertS(in *pb.InsertSReq) (*pb.InsertSReps, error) {
	_, err := l.svcCtx.ScoreMoedl.FindOne(l.ctx, in.UID)
	msg := ""
	s1 := model.Score{
		Uid:    "null",
		Gender: "null",
	}
	if err != nil {
		s1.Uid = in.UID
		s1.Gender = in.Gender
		ret, err := l.svcCtx.ScoreMoedl.Insert(l.ctx, &s1)
		if err != nil {
			fmt.Println("ret", ret, "err", err)
			msg = "插入失败"
		} else {
			msg = "插入成功"
		}
	} else {
		fmt.Println(err)
		msg = "UID重复"
	}
	return &pb.InsertSReps{
		Msg:    msg,
		UID:    s1.Uid,
		Gender: s1.Gender,
	}, nil
}
